from rest_framework.filters import OrderingFilter, SearchFilter
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.viewsets import ModelViewSet

from course import models, serializer

from .paginations import LuffyPagination
from .filters import CourseFilterSet


class CourseCategoryView(ModelViewSet):
    queryset = models.CourseCategory.objects.filter(
        is_delete=False, is_show=True).order_by("orders")
    serializer_class = serializer.CourseCategorySerializer


class CoursesView(ModelViewSet):
    queryset = models.Course.objects.filter(
        is_delete=False, is_show=True).order_by("orders")
    serializer_class = serializer.CourseModelSerializer
    pagination_class = LuffyPagination
    filter_backends = [DjangoFilterBackend, OrderingFilter]
    ordering_fields = ['id', 'price', 'students']
    filter_class = CourseFilterSet


class CourseChapterView(ModelViewSet):
    queryset = models.CourseChapter.objects.filter(
        is_delete=False, is_show=True)
    serializer_class = serializer.CourseChapterSerializer
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']


class CourseSearchView(ModelViewSet):
    queryset = models.Course.objects.filter(
        is_delete=False, is_show=True).order_by("orders")
    serializer_class = serializer.CourseModelSerializer
    pagination_class = LuffyPagination
    filter_backends = [SearchFilter]
    serach_fields = ["name"]  # 搜索字段，?search=<name>